'**************************************************************** '* Name : UNTITLED.BAS * '* Author : [select VIEW...EDITOR OPTIONS] * '* Notice : Copyright (c) 2006 [select VIEW...EDITOR OPTIONS] * '* : All Rights Reserved * '* Date : 3/28/2006 * '* Version : 1.0 * '* Notes : * '* : * '**************************************************************** DEFINE osc 4 output PORTC.0 output PORTC.1 output PORTC.2 output PORTC.3 output PORTC.4 output PORTD.3 output PORTD.1 'define DataPin, ClockPin, Load Pin D0PIN1 var PORTC.0 D0PIN2 var PORTC.1 D0PIN3 var PORTC.2 D0PIN4 var PORTC.3 D0PIN5 var PORTC.4 CPIN var PORTD.3 IOPIN var PORTD.1 position var byte [3] nextPosition var Byte [3] target var byte [3] newTarget var byte XYorZ var Byte direction var bit changeDirection var bit randNum var word newTarget = 1 position[0] = 0 position[1] = 0 position[2] = 0 'variables to put the current row, current column tempRow var byte tempCol var byte 'variables for all on/ all of on a certain row ALL_ON var byte ALL_OFF var byte i var byte j var byte ' array to hold the columns, rows, threshold values, serial input arrayColP VAR BYTE ' All OFF arrayColA VAR BYTE arrayColB VAR BYTE arrayColC VAR BYTE arrayColD VAR BYTE arrayRow var byte[8] arrayCol var byte[8] high portb.7 pause 1000 low portb.7 pause 1000 high portb.7 pause 1000 low portb.7 pause 1000 '============ Initialization of program happens here ============' ALL_ON = $FF ALL_OFF = $00 ' pauseDuration = 10 arrayCol[0] = %10000000 ' All OFF arrayCol[1] = %01000000 arrayCol[2] = %00100000 arrayCol[3] = %00010000 arrayCol[4] = %00001000 arrayRow [0] = %00000001 arrayRow [1] = %00000010 arrayRow [2] = %00000011 arrayRow [3] = %00000100 arrayRow [4] = %00000101 'XXXXAAAA 00000000 high IOPIN 'set chip select line high PAUSE 10 'first worry about decode mode. on power up device does not decode 'so we do not need to do anything serout2 portc.6, 16468, ["scanlimit", 13, 10] pause 5000 ' set the scan limit to xxxx1011xxxxxxxx (display Digit0,1,2,3,4,5,6,7 xxxxx111) low IOPIN 'SHIFTOUT D0PIN1, CPIN, 1, [%0000101100000000\16] 'SHIFTOUT D0PIN2, CPIN, 1, [%0000101100000000\16] 'SHIFTOUT D0PIN3, CPIN, 1, [%0000101100000000\16] SHIFTOUT D0PIN4, CPIN, 1, [%0000101100000100\16] 'SHIFTOUT D0PIN5, CPIN, 1, [%0000101100000000\16] high IOPIN PAUSE 2000 serout2 portc.6, 16468, ["intensity", 13, 10] 'set the intensity register, initially lights are at full intensity. low IOPIN 'SHIFTOUT D0PIN1, CPIN, 1, [%0000101000001111\16] 'SHIFTOUT D0PIN2, CPIN, 1, [%0000101000001111\16] 'SHIFTOUT D0PIN3, CPIN, 1, [%0000101000001111\16] SHIFTOUT D0PIN4, CPIN, 1, [%0000101000001111\16] 'SHIFTOUT D0PIN5, CPIN, 1, [%0000101000001111\16] high IOPIN PAUSE 2000 serout2 portc.6, 16468, ["shutdown", 13, 10] 'disable shutdown mode xxxx1100xxxxxxx1 (shutdown mode xxxx1100xxxxxxx0) low IOPIN 'SHIFTOUT D0PIN1, CPIN, 1, [%0000110000000001\16] 'SHIFTOUT D0PIN2, CPIN, 1, [%0000110000000001\16] 'SHIFTOUT D0PIN3, CPIN, 1, [%0000110000000001\16] SHIFTOUT D0PIN4, CPIN, 1, [%0000110000000001\16] 'SHIFTOUT D0PIN5, CPIN, 1, [%0000110000000001\16] high IOPIN PAUSE 2000 serout2 portc.6, 16468, ["display", 13, 10] 'disable display mode xxxx1111xxxxxxx0 (display mode xxxx1100xxxxxxx1) low IOPIN 'SHIFTOUT D0PIN1, CPIN, 1, [%0000111100000000\16] 'SHIFTOUT D0PIN2, CPIN, 1, [%0000111100000000\16] 'SHIFTOUT D0PIN3, CPIN, 1, [%0000111100000000\16] SHIFTOUT D0PIN4, CPIN, 1, [%0000111100000000\16] 'SHIFTOUT D0PIN5, CPIN, 1, [%0000111100000000\16] high IOPIN PAUSE 2000 serout2 portc.6, 16468, ["alloff", 13, 10] 'turn them all off, so that we start off clean For i = 0 To 4 low IOPIN SHIFTOUT D0PIN1, CPIN, 1, [arrayRow[i], ALL_OFF] high IOpin Pause 20 Low IOpin SHIFTOUT D0PIN2, CPIN, 1, [arrayRow[i], ALL_OFF] high IOpin Pause 20 Low IOpin SHIFTOUT D0PIN3, CPIN, 1, [arrayRow[i], ALL_OFF] high IOpin Pause 20 Low IOpin SHIFTOUT D0PIN4, CPIN, 1, [arrayRow[i], ALL_OFF] high IOpin Pause 20 Low IOpin SHIFTOUT D0PIN5, CPIN, 1, [arrayRow[i], ALL_OFF] high IOPIN Next i PAUSE 10 high portb.7 pause 1000 low portb.7 pause 1000 high portb.7 pause 1000 low portb.7 pause 1000 main: serout2 portc.6, 16468, ["in main"] 'this sends us to a subroutine to generate a new target where the fly aspires to go. 'this happens at startup and whenever the fly reaches its target if newTarget == 1 then goto targetgen ENDIF 'this random number decides whether the fly will move in x, y or z axis random randNum if randNum > 21845 then XYorZ = 1 if randNum > 43690 then XYorZ = 2 endif ELSE XYorZ = 0 endif 'this random number determines whether the fly will be successful in trying to move towards the target 'the fly has a 67% chance of success right now random randNum if randNum > 43690 then changeDirection = 1 else changeDirection = 0 endif 'This determines the direction the fly needs to move in order to reach its target if position[XYorZ] >= Target[XYorZ] then direction = 0 else direction = 1 endif 'if change direction is true (which of course gets decided randomly above) 'then reverse the direction in which it was headed ' I can see that this annoys you, Shagun. The reason for all of this is that the fly's direction is not in 'fact completely random. it behaves based upon weighted chances. The reason this looks redundant is that 'I am attempting to make the fly seem more like an actual living creature instead of a randomly moving LED. if changeDirection == 1 then direction = ! direction endif 'this defines the position of the fly in the next iteration depending on where it is currently 'the reason it is calculated this way is to keep it from moving more than one space at a time. if direction == 0 then nextPosition[XYorZ] = position[xyorz] - 1 else nextposition[xyorz] = position[xyorz] + 1 endif 'this keeps the fly from escaping the box 'a line or lines of code could also be added here to trigger the solenoid if nextPosition[XYorZ] < 0 then nextposition[xyorz] = 0 endif if nextposition[xyorz] > 4 then nextposition[xyorz] = 4 endif ' load the newly calculated position into the memory before sending commands to the LEDs for i = 0 to 2 position[i] = nextposition[i] next i 'if the positions are equal to the suggested targets then generate a random target if position[0] == target[0] and position[1] == target[1] and position[2] == target[2] then newTarget = 1 endif 'spit out the positions serout2 portc.6, 16468, ["position ", DEC position[0], DEC position[1], DEC position[2], 13, 10 ] ' puts all the LED's off for i = 0 to 4 low IOpin shiftout D0PIN1, CPIN, 1, [arrayRow[i], ALL_OFF] low IOpin high IOPIN shiftout D0PIN2, CPIN, 1, [arrayRow[i], ALL_OFF] low IOpin high IOPIN shiftout D0PIN3, CPIN, 1, [arrayRow[i], ALL_OFF] low IOpin high IOPIN shiftout D0PIN4, CPIN, 1, [arrayRow[i], ALL_OFF] low IOpin high IOPIN shiftout D0PIN5, CPIN, 1, [arrayRow[i], ALL_OFF] high IOPIN next i serout2 portc.6, 16468, ["pause", 13, 10] 'this next line of code is for testing purposes on one board. It should be removed as soon as multiple 'boards are hooked up Position[2] = 3 low IOpin 'this sends the command to turn on one LED to whichever board the LED belongs to SElect CASE position[2] case 0 shiftout D0PIN1, CPIN, 1, [arrayRow[position[0]], arrayCol[position[1]]] case 1 shiftout D0PIN2, CPIN, 1, [arrayRow[position[0]], arrayCol[position[1]]] case 2 shiftout D0PIN3, CPIN, 1, [arrayRow[position[0]], arrayCol[position[1]]] case 3 shiftout D0PIN4, CPIN, 1, [arrayRow[position[0]], arrayCol[position[1]]] case 4 shiftout D0PIN5, CPIN, 1, [arrayRow[position[0]], arrayCol[position[1]]] end select high iopin pause 50 GoTo main targetgen: for i = 0 to 2 random randNum target[i] = randNum/13107 next i newTarget = 0 goto main